package com.colin.library.help;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.telephony.TelephonyManager;

import java.io.File;
import java.lang.reflect.Field;

/**
 * Created by Administrator on 2016/6/28.
 */
public class AndroidHelp {

    private static volatile AndroidHelp singleton;

    private AndroidHelp() {}

    public static AndroidHelp getInstance() {
        if (singleton == null) {
            synchronized (AndroidHelp.class) {
                if (singleton == null) {
                    singleton = new AndroidHelp();
                }
            }
        }
        return singleton;
    }
    /**
     * 安装apk
     * 如果没有android.os.Process.killProcess(android.os.Process.myPid());最后不会提示完成、打开。
     * 如果没有i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);这一步的话，最后安装好了，点打开，是不会打开新版本应用的。
     *
     * @param context
     * @param apk_file_path 路径和文件名
     */
    public  void installApk(Context context, String apk_file_path) {
        File apkfile = new File(apk_file_path);
        if (!apkfile.exists()) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
        context.startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public  int getVersionCode(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), -1);
            int version = info.versionCode;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public  String getVersionName(Context context) {
        String versionName = "";
        try {
            versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    public  String getDeviceId(Context context) {
        String deviceId = "no device id";
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            deviceId = telephonyManager.getDeviceId();
        } catch (Exception e) {
            e.printStackTrace();
            deviceId = "no device id";
        } finally {
            return deviceId;
        }
    }


    public  String getBuildInfo() {
        StringBuilder sb = new StringBuilder();
        Field[] fields = Build.class.getDeclaredFields();
        try {
            for (Field f : fields) {
                sb.append(f.getName() + ":" + f.get(null) + "\n");
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public  int getBuildSdkInt() {
        return Build.VERSION.SDK_INT;
    }

    public  String getVersionSdkString() {
        return Build.VERSION.SDK;
    }


}
